package cate.game.role.farm.adventure;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDPosition;
import cate.common.table.farm.adventure.row.AdventureBossRow;
import cate.common.util.GameResult;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.farm.AbstractFarm;
import easy.java.practice.errorcode.EcResult;

public class AdventureBoss extends AbstractFarm {

    public int bossPassedId;

    public GameResult<Void> challenge() {
        GameResult<Void> r = new GameResult<>();
        if (fighting) {
            return r.fail("当前战斗未结束");
        }
        // 目的关卡ID
        int tarCombatId = this.bossPassedId + 1;
        AdventureBossRow tpl = role.getGame().table.adventure.boss.get(tarCombatId);
        if (tpl == null) {
            return r.fail("你已经战胜了所有boss");
        }
        if (role.getFarm().adventure.getAdventurePassedNum() < tpl.adventureId) {
            return r.fail("BOSS通关要求:{}关", tpl.adventureId);
        }
        return doChallenge(tpl, GDPosition.DEFAULT_POS_NUM);
    }

    public GameResult<Void> doChallenge(AdventureBossRow tpl, byte posNum) {
        GameResult<Void> r = new GameResult<>();
        AdventureBossFTC context = new AdventureBossFTC(role, tpl);
        context.playStory.level = tpl.id;
        context.playStory.playIndex = posNum;
        EcResult<FightSide> fsr_A = role.getGame().fight.side.createFightSideByRole(GDFight.Team.A, role, GDFunc.ADVENTURE);
        if (!fsr_A.ok()) {
            return r.fail(fsr_A);
        }
        EcResult<FightSide> fsr = role.getGame().fight.side.createFightSideByCode(GDFight.Team.B, tpl.monster);
        if (!fsr.ok()) {
            return r.fail(fsr);
        }
        EcResult<PlayPO> play_r = role.getGame().play.startByRole(role, context, fsr_A.data, fsr.data);
        if (!play_r.ok()) {
            return r.fail(play_r);
        }
        this.fighting = true;
        return r.success();
    }

    @Override
    public int getFuncId() {
        return GDFunc.ADVENTURE_BOSS;
    }

    @Override
    public void onAfterOpen() {

    }

    @Override
    public int getFightFuncId() {
        return GDFunc.ADVENTURE_BOSS;
    }

    public void changeBossIdPassed(int bossPassedId) {
        this.bossPassedId = bossPassedId;
//        publishEvent(role, new AdventurePassedIdChangeEvent(role, bossPassedId));
    }

    public AdventureBossRow getBossRow() {
        return role.getGame().table.adventure.boss.get(bossPassedId);
    }
}
